#RemoteView 自定义桌面小部件
效果如上图, class AppWidgetProvider extends BroadcastReceiver,不难看出桌面小部件本质上是一个广播,至于为什么是一个广播的形势,我猜…是因为桌面小部件运行在SystemService进程中,通过PendingIntent这种方式传递,避免了自己写IPC通信。
#自定义小部件统共分几步
###1.定义小部件界面
在layout 下创建xml 文件
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/tv_content"
android:src="@mipmap/ic_launcher"
android:gravity="center"
android:textSize="20sp"
android:layout_width="100dp"
android:layout_height="100dp" />
</RelativeLayout>
###2.定义小部件配置信息
通过as自带的创建app widget功能会自动创建该文件,否则需要手动指定,定义小部件刷新,最小宽高等信息
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/new_app_widget"
android:initialLayout="@layout/new_app_widget"
android:minHeight="110dp"
android:minWidth="110dp"